﻿using System.IO;
using LoungeRepo.Contracts.EntityStore;

namespace LoungeRepo.EntityStore
{
    partial class FilesystemEntityStore
    {
        private Stream CreateEntityFile(IStreamedEntity streamedEntity)
        {
            var entityFilename = CreateEntityFilename(streamedEntity.Id, streamedEntity.Partition);
            CreateDirectory(Path.GetDirectoryName(entityFilename));
            return new FileStream(entityFilename, FileMode.Create);
        }


        private string CreateEntityFilename(string id, string partition)
        {
            return string.Format(@"{0}\{1}\{2}.entity", this.rootFolder, partition, id);
        }


        private void CreateDirectory(string dirPath)
        {
            if (!Directory.Exists(dirPath))
                Directory.CreateDirectory(dirPath);
        }
    }
}
